<?php
require_once ("../config/path_config.php");
require_once (ROOTDIR."admin/admin-header.php");
require_once(ROOTDIR."include/check_post_key.php");
require_once (ROOTDIR."include/problem.php");
require_once (ROOTDIR."config/sys_config.php");
require_once (ROOTDIR."config/db_config.php");
require_once (ROOTDIR."common/db_utils.php");

// 权限判断
$allowAccess = isset($_SESSION['administrator']);
$allowAccess |= isset($_SESSION['problem_editor']);
if ( !$allowAccess ){
	echo "不具备访问该页面的权限";
	exit(1);
}
?>

<?php
// 获取参数
$title = $_POST ['title'];
$time_limit = $_POST ['time_limit'];
$memory_limit = $_POST ['memory_limit'];
$description = $_POST ['description'];
$input = $_POST ['input'];
$output = $_POST ['output'];
$sample_input = $_POST ['sample_input'];
$sample_output = $_POST ['sample_output'];
$test_input = isset($_POST ['test_input']) ? $_POST ['test_input'] : null;
$test_output = isset($_POST ['test_output']) ? $_POST ['test_output'] : null;
$hint = $_POST ['hint'];
$source = $_POST ['source'];
$spj = $_POST ['spj'];
$level = isset($_POST ['level']) ? $_POST ['level'] : 1;
$fromoj = $_POST ['fromoj']; // 题目来源

if (get_magic_quotes_gpc ()) {
	$title = stripslashes ( $title);
	$time_limit = stripslashes ( $time_limit);
	$memory_limit = stripslashes ( $memory_limit);
	$description = stripslashes ( $description);
	$input = stripslashes ( $input);
	$output = stripslashes ( $output);
	$sample_input = stripslashes ( $sample_input);
	$sample_output = stripslashes ( $sample_output);
	$test_input = stripslashes ( $test_input);
	$test_output = stripslashes ( $test_output);
	$hint = stripslashes ( $hint);
	$source = stripslashes ( $source);
	$spj = stripslashes ( $spj);
	$level = stripslashes ( $level );
    $fromoj = stripslashes ( $fromoj);
}

//添加题目
$pid = addproblem($title, $time_limit, $memory_limit,
    $description, $input, $output, $sample_input, $sample_output,
    $hint, $source, $level, $spj, SysConfig::$OJ_DATA, $fromoj );

// 创建测试数据文件夹
$basedir = SysConfig::$OJ_DATA."/".$pid;
mkdir ( $basedir );

// 创建样例数据和测试数据
if( strlen($sample_output) &&! strlen($sample_input) )
    $sample_input="0";
if( strlen($sample_input) )
    mkdata($pid,"sample.in", $sample_input, SysConfig::$OJ_DATA);
if(strlen($sample_output))
    mkdata($pid,"sample.out", $sample_output, SysConfig::$OJ_DATA);
if(strlen($test_output)&&!strlen($test_input))
    $test_input="0";
if( isset($test_input) && strlen($test_input) ) {
    mkdata($pid,"test0.in", $test_input, SysConfig::$OJ_DATA);
}
if( isset($test_output) && strlen($test_output) ) {
    mkdata($pid,"test0.out", $test_output, SysConfig::$OJ_DATA);
}

echo "<a href='../problem.php?id=".$pid."'>See the problem</a><br>";
echo "<a href='javascript:phpfm($pid);'>Add more TestData now !</a>";
?>

<script>
function phpfm(pid) {
    $.post("phpfm.php",
        {'frame':3,'pid':pid,'pass':''},
        function(data, status){
            if(status=="success") {
                document.location.href="phpfm.php?frame=3&pid="+pid;
            }
        });
}
</script>
